package com.qst.order.controller;


import com.qst.order.domain.VO.ProductRankVO;
import com.qst.order.domain.VO.StatusDistributionVO;
import com.qst.order.domain.VO.SummaryVO;
import com.qst.order.domain.VO.TrendVO;
import com.qst.order.service.StatisticsService;
import com.qst.order.utils.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/api/statistics")
@RequiredArgsConstructor
public class StatisticsController {

    private final StatisticsService statisticsService;

    // 概览统计
    @GetMapping("/summary")
    public Result<SummaryVO> getSummaryStatistics() {
        SummaryVO data = null;
        try {
            data = statisticsService.getSummaryStatistics();
        } catch (Exception e) {
            log.error("获取概览统计失败：{}", e.getMessage());
            throw new RuntimeException(e);
        }
        return Result.success(data);
    }

    // 订单趋势
    @GetMapping("/order/trend")
    public Result<List<TrendVO>> getOrderTrend(
            @RequestParam LocalDate startDate,
            @RequestParam LocalDate endDate) {
        List<TrendVO> data = null;
        try {
            data = statisticsService.getOrderTrend(startDate, endDate);
        } catch (Exception e) {
            log.error("获取订单趋势失败：{}", e.getMessage());
            throw new RuntimeException(e);
        }
        return Result.success(data);
    }

    // 销售额趋势
    @GetMapping("/sales/trend")
    public Result<List<TrendVO>> getSalesTrend(
            @RequestParam LocalDate startDate,
            @RequestParam LocalDate endDate) {
        List<TrendVO> data = null;
        try {
            data = statisticsService.getSalesTrend(startDate, endDate);
        } catch (Exception e) {
            log.error("获取销售额趋势失败：{}", e.getMessage());
            throw new RuntimeException(e);
        }
        return Result.success(data);
    }

    // 订单状态分布
    @GetMapping("/order/status")
    public Result<List<StatusDistributionVO>> getOrderStatusDistribution() {
        List<StatusDistributionVO> data = null;
        try {
            data = statisticsService.getOrderStatusDistribution();
        } catch (Exception e) {
            log.error("获取订单状态分布失败：{}", e.getMessage());
            throw new RuntimeException(e);
        }
        return Result.success(data);
    }

    // 商品排行
    @GetMapping("/product/rank")
    public Result<List<ProductRankVO>> getProductRank(@RequestParam String type) {
        List<ProductRankVO> data = null;
        try {
            data = statisticsService.getProductRank(type);
        } catch (Exception e) {
            log.error("获取商品排行失败：{}", e.getMessage());
            throw new RuntimeException(e);
        }
        return Result.success(data);
    }
}